home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / pc / files / t_unix / j109lxa4.tar / pop2cli.c < prev    next >
C/C++ Source or Header  |  1994-06-04  |  4KB  |  164 lines

  1. /* POP2 Client routines -- Not recommended.
  2.  *      Jan 92  William Allen Simpson
  3.  *              complete re-write to match new mailreader commands
  4.  *
  5.  *      partly based on a NNTP client design by Anders Klemets, SM0RGV
  6.  *      Originally authored by Mike Stockett (WA7DYX).
  7.  *
  8.  * History:
  9.  *      Modified 12 May 1991 by Mark Edwards (WA6SMN) to use new timer
  10.  *      facilities in NOS0423.  Fixed type mismatches spotted by C++.
  11.  *      Modified 27 May 1990 by Allen Gwinn (N5CKP) for compatibility
  12.  *        with later releases (NOS0522).
  13.  *      Added into NOS by PA0GRI (and linted into "standard" C)
  14.  *      Modified 14 June 1987 by P. Karn for symbolic target addresses,
  15.  *        also rebuilt locking mechanism
  16.  *
  17.  *      Some code culled from previous releases of SMTP.
  18.  *      See that code for applicable copyright notices.
  19.  */
  20. #include <stdio.h>
  21. #include <time.h>
  22. #include "global.h"
  23. #include "timer.h"
  24. #include "proc.h"
  25. #include "netuser.h"
  26. #include "socket.h"
  27. #include "cmdparse.h"
  28. #include "files.h"
  29. #include "mailcli.h"
  30. #include "mailutil.h"
  31. #include "smtp.h"
  32.  
  33. #ifdef POP2CLIENT
  34.  
  35. extern char Badhost[];
  36.  
  37. /* Response string keys */
  38. static char *greeting_rsp  = "+ POP2 ";
  39.  
  40. void
  41. pop2_job(unused,v1,p2)
  42. int unused;
  43. void *v1;
  44. void *p2;
  45. {
  46.     struct mailservers *np = v1;
  47.     struct sockaddr_in fsocket;
  48.     char buf[TLINELEN];
  49.     char *cp;
  50.     FILE *wfp = NULLFILE;
  51.     int s = -1;
  52.     int folder_len;
  53.     int msg_num = 0;
  54.  
  55.     if ( mailbusy( np ) )
  56.         return;
  57.  
  58.     if ( (fsocket.sin_addr.s_addr = resolve(np->hostname)) == 0L ) {
  59.         /* No IP address found */
  60.         if (Mailtrace >= 1)
  61.             log(-1,"POP2 can't resolve host '%s'", np->hostname);
  62.         start_timer(&np->timer);
  63.         return;
  64.     }
  65.  
  66.     fsocket.sin_family = AF_INET;
  67.     fsocket.sin_port = IPPORT_POP2;
  68.  
  69.     s = socket(AF_INET,SOCK_STREAM,0);
  70.     sockmode(s,SOCK_ASCII);
  71.  
  72.     if (connect(s,(char *)&fsocket,SOCKSIZE) == -1) {
  73.         cp = sockerr(s);
  74.         if (Mailtrace >= 2)
  75.             log(s,"POP2 Connect failed: %s",
  76.                 cp != NULLCHAR ? cp : "");
  77.         goto quit;
  78.     }
  79.  
  80.     log(s,"POP2 Connected to mailhost %s", np->hostname);
  81.  
  82.     if ( mailresponse( s, buf, "banner" ) == -1)
  83.         goto quit;
  84.  
  85.     if (strncmp(buf,greeting_rsp,strlen(greeting_rsp)) != 0)
  86.         goto quitcmd;
  87.  
  88.     (void)usprintf(s,"HELO %s %s\n",np->username,np->password);
  89.  
  90.     if ( mailresponse( s, buf, "HELO" ) == -1)
  91.         goto quit;
  92.  
  93.     if (buf[0] != '#'
  94.       || (folder_len = atoi(&(buf[1]))) == 0) {
  95.         /* If there is no mail (the only time we get a "+"
  96.          * response back at this stage of the game),
  97.          * then just close out the connection, because
  98.          * there is nothing more to do!! */
  99.         goto quitcmd;
  100.     }
  101.  
  102.     if ((wfp = tmpfile()) == NULLFILE) {
  103.         if ( Mailtrace >= 1 )
  104.             log(s,"POP2 Cannot create %s", "tmp file" );
  105.         goto quitcmd;
  106.     }
  107.  
  108.     (void)usprintf(s,"READ\n");
  109.  
  110.     /* now, get the text */
  111.     while(TRUE) {
  112.         long msg_len;
  113.  
  114.         if ( mailresponse( s, buf, "read loop" ) == -1)
  115.             goto quit;
  116.  
  117.         if (buf[0] == '='
  118.          && (msg_len = atol(&(buf[1]))) > 0) {
  119.             (void)usprintf(s,"RETR\n");
  120.  
  121.             while ( msg_len > 0 ) {
  122.                 if (recvline(s,buf,TLINELEN) == -1)
  123.                     goto quit;
  124.  
  125.                 rip(buf);
  126.                 fprintf(wfp,"%s\n",buf);
  127.  
  128.                 msg_len -= (long)(strlen(buf)+2);/* Add CRLF */
  129.             }
  130.             (void)usprintf(s,"ACKD\n");
  131.  
  132.             msg_num++;
  133.         } else {
  134.             break;
  135.         }
  136.     }
  137.  
  138.     if ( folder_len > 0 ) {
  139.         /* testing the result is pointless,
  140.          * since POP2 already deleted mail.
  141.          */
  142.         copymail( Mailspool, np->mailbox, buf, TLINELEN, wfp, Mailtrace );
  143.  
  144.         tprintf("New mail arrived for %s from mailhost %s%c\n",
  145.                 np->mailbox,
  146.                 np->hostname,
  147.                 Mailquiet ? ' ' : '\007');
  148.     }
  149.  
  150. quitcmd:
  151.     (void)usprintf(s,"QUIT\n");
  152.  
  153. quit:
  154.     log(s,"POP2 daemon exiting");
  155.     (void) close_s(s);
  156.  
  157.     if (wfp != NULLFILE)
  158.         fclose(wfp);
  159.     start_timer(&np->timer);
  160. }
  161.  
  162. #endif
  163.  
  164.